package com.coolingme.trycatch;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 使用完IO资源流，需要关闭
 *
 * @author wangyue
 * @date 2020/12/3 10:09
 */
public class TryDemo {

    public static void main(String[] args) {
        // try-with-resource
        try (FileInputStream in = new FileInputStream("D://111.txt")) {
            in.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 使用finally关闭
        FileInputStream in2 = null;
        try {
            in2 = new FileInputStream("D://111.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (in2 != null) {
                try {
                    in2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
